import requests

from bs4 import BeautifulSoup

# 1
url = "https://mpapress.com/"
response = requests.get(url)
html_content = response.text

# 3
soup = BeautifulSoup(html_content, 'html.parser')

#  提取所需信息
latest_first_head = soup.find(id="latest-first-head")
container = latest_first_head.find('div', class_='jeg_postblock_content')
indict = {
    'title': '', 'title_url': '',
    'author': '', 'author_url': '',
    'time': ''
}


# 标题
def get_title(html):
    title = html.find('h3', class_='jeg_post_title')
    _a_tag = title.find('a')
    if _a_tag:
        indict['title'] = _a_tag.text
        indict['title_url'] = _a_tag.get('href')


# 作者
def get_author(html):
    author = html.find('div', class_="jeg_meta_author")
    _a_tag = author.find('a')
    if _a_tag:
        indict['author'] = _a_tag.text
        indict['author_url'] = _a_tag.get('href')


# 时间
def get_time(html):
    time = html.find('div', class_="jeg_meta_date")
    _a_tag = time.find('a')
    if _a_tag:
        indict['time'] = _a_tag.text


# 评论数
def get_content(html):
    view = html.find('div', class_="jeg_meta_comment")
    _a_tag = view.find('a')
    if _a_tag:
        title_name = _a_tag.text
        link = _a_tag.get('href')
        print(f"评论数: {title_name}, 链接: {link}")


# 阅读量
def get_views(html):
    views = html.find('div', class_="jeg_meta_views")
    for view in views:
        _a_tag = view.find('a')
        if _a_tag:
            title_name = _a_tag.text
            link = _a_tag.get('href')
            print(f"阅读量: {title_name}, 链接: {link}")
